package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.CheckGroupItemDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.CustomizeException;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;


@Slf4j
@DubboService
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private CheckGroupItemDao checkGroupItemDao;

    @Override
    public boolean add(CheckItem checkItem) {
        /**
         * insert：全部字段进行拼接
         * insertSelective：只有传入的字段进行SQL拼接
         */
        boolean flag = false;
        int insert = checkItemDao.insertSelective(checkItem);

        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public long findTotal() {
        CheckItem checkItem = new CheckItem();
        return checkItemDao.selectCount(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        /**
         * PageHelper分页插件传入传入当前页码，条数
         */
        PageResult pageResult = null;
        //redis中没有数据则从数据库中获取
        if (!redisTemplate.hasKey(RedisConst.FIND_CHECKITEM_PAGE)){
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

            Page<CheckItem> page = checkItemDao.findPage(queryPageBean.getQueryString());

            pageResult = new PageResult(page.getTotal(), page.getResult());
            redisTemplate.boundValueOps(RedisConst.FIND_CHECKITEM_PAGE).set(pageResult);
        }else {
            pageResult = (PageResult) redisTemplate.boundValueOps(RedisConst.FIND_CHECKITEM_PAGE).get();
        }
        return pageResult;
    }

    @Override
    public Integer delete(Integer id) {
        log.info("[检查项-根据id删除]id：{}",id);
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckitemId(id);
        int count = checkGroupItemDao.selectCount(checkGroupItem);
        if (count > 0){
            throw new CustomizeException("该检查项已绑定检查组不可删除");
        }
        return checkItemDao.deleteByPrimaryKey(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.selectByPrimaryKey(id);
    }

    @Override
    public boolean update(CheckItem checkItem) {
        boolean flag = false;
        int insert = checkItemDao.updateByPrimaryKeySelective(checkItem);
        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = null;
        if (!redisTemplate.hasKey(RedisConst.FIND_CHECKITEM_ALL)){
            checkItemList = checkItemDao.selectAll();
            redisTemplate.boundValueOps(RedisConst.FIND_CHECKITEM_ALL).set(checkItemList);
        }else {
            checkItemList = (List<CheckItem>) redisTemplate.boundValueOps(RedisConst.FIND_CHECKITEM_ALL).get();
        }
        return checkItemList;
    }


}
